package cn.jbit.commodity.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.pagehelper.PageInfo;

import cn.jbit.commodity.service.CommodityService;
import cn.jbit.pojo.User;
import cn.jbit.pojo.commodity;
import cn.jbit.pojo.commodity_details;
import cn.jbit.pojo.commodity_type;

@Controller
public class CommodityController {

	private static final Logger LOGGER = LoggerFactory.getLogger(CommodityController.class);

	@Resource
	private CommodityService commodityService;

	/**
	 * 进入商品管理请求
	 * 
	 * @return
	 */
	@RequestMapping("inventory")
	public String goinventory() {
		return "inventory";
	}

	/**
	 * 进入编辑商品页面
	 */
	@RequestMapping("editcommodity")
	public String goeditcommodity(Model model, Integer commodity_id) {
		// 查询需要编辑的商品所有信息
		PageInfo<commodity> comm = commodityService.fuzzyQueryOfCommodityNames(null, 1, null, null, null, commodity_id);
		// 查询编辑商品信息页面的下拉框
//		List<commodity_type> types = commodityService.queryCommodityType();
//		model.addAttribute("commodity_type", types);
		model.addAttribute("commodity", comm.getList().get(0));
		return "editcommodity";
	}

	/**
	 * 查询下拉框值，商品类型列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("queryCommodityId")
	@ResponseBody
	public Object queryCommodityType() {
		System.out.println("获取下拉");
		List<commodity_type> types = commodityService.queryCommodityType();
		return types;
	}

	/**
	 * 分页查询所有商品或分页模糊查询商品或通过供应商id查询或通过商品类型查询
	 * 
	 * @return
	 */
	@RequestMapping("commodityList1")
	@ResponseBody
	public Object gocommodityList1(String gocommoditName, Integer pageNum, Integer userId, Integer commodity_type_id,
			Integer commodity_state) {

		// 判斷是否第一次進入方法
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}

		PageInfo<commodity> list = commodityService.fuzzyQueryOfCommodityNames(gocommoditName, pageNum, userId,
				commodity_type_id, commodity_state, null);
		return list;
	}

	@RequestMapping("commodityList")
	public String gocommodityList(String gocommoditName, Integer pageNum, Integer userId, Integer commodity_type_id,
			Integer commodity_state, Model model) {

		// 判斷是否第一次進入方法
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}

//		List<commodity> list = commodityService.fuzzyQueryOfCommodityNames(gocommoditName, pageNum, userId,
//				commodity_type_id, commodity_state, null);
//		model.addAttribute("commodityList", list);
		return "forward:queryCommodityId";
	}

	/**
	 * 
	 * 异步修改商品信息
	 * 
	 */
	@RequestMapping("modifyCommodity")
	@ResponseBody
	public String goModifyCommodity(@RequestParam(value="file") MultipartFile file,commodity comm) {
		if (file.isEmpty()) {
			return "false";
		}
		// 获取原始文件名
		String fileName = file.getOriginalFilename();
		int in = (int) (1 + Math.random() * (10 - 1 + 10000000));
		fileName = in + fileName;
		String courseFile = "";
		File directory = new File("");// 参数为空
		try {
			courseFile = directory.getCanonicalPath();// 标准的路径 ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		String author = directory.getAbsolutePath();// 绝对路径;
		// 这里将路径跟文件名结合而成的文件名传入
		File dest = new File(author + "\\src\\main\\resources\\static\\commodityimg\\" + fileName);
		try {
			file.transferTo(dest);
			LOGGER.info("上传成功");
			comm.setCommodity_img(fileName);
			commodityService.modifyCommodity(comm);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
	}

	/**
	 * 异步添加商品
	 * 
	 */
	@RequestMapping("addCommodity")
	@ResponseBody
	public String goAddCommodity(/*@RequestParam(value="file") MultipartFile file,*/commodity comm, HttpSession session) {

		User user = new User();
		// 获取session里面的用户信息
		user = (User) session.getAttribute("user");
		// 保存用户id
		comm.setCommodity_user_id(user.getUser_id());

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		// 保存商品发布时间
		comm.setCommodity_time(df.format(new Date()).toString());

		comm.setCommodity_quantitysold(0);
		//添加的商品状态
		comm.setCommodity_state(3);
		
//		if (file.isEmpty()) {
//			return "false";
//		}
		// 获取原始文件名
//		String fileName = file.getOriginalFilename();
//		int in = (int) (1 + Math.random() * (10 - 1 + 10000000));
//		fileName = in + fileName;
//		String courseFile = "";
//		File directory = new File("");// 参数为空
//		try {
//			courseFile = directory.getCanonicalPath();// 标准的路径 ;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		String author = directory.getAbsolutePath();// 绝对路径;
//		// 这里将路径跟文件名结合而成的文件名传入
//		File dest = new File(author + "\\src\\main\\resources\\static\\commodityimg\\" + fileName);


		try {
//			file.transferTo(dest);
//			LOGGER.info("上传成功");
//			comm.setCommodity_img(fileName);
			commodityService.addCommodity(comm);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}

//		return null;

	}
	
	
	/**
	 * 异步添加商品类型
	 */
	@RequestMapping("addCommodityType")
	@ResponseBody
	public String goAddCommodityType(String commoditytype_name) {
		try {
			commodityService.addCommodityType(commoditytype_name);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
	}
	
	/**
	 * 异步修改商品状态
	 */
	@RequestMapping("modifyState")
	@ResponseBody
	public String goModifyState(Integer id,Integer state) {
		try {
			commodityService.modifyState(id, state);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
	}
	
	/**
	 * 
	 * 进入详情页
	 */
	@RequestMapping("product_detail_page")
	public String goproduct_detail_page(Integer id,Model model) {
		model.addAttribute("id",id);
		return "product_detail_page";
	}
	
	/**
	 * 
	 * 一异步查询详情页
	 */
	@RequestMapping("queryDetails")
	@ResponseBody
	public commodity queryDetails(Integer id) {
		try {
			commodity comm = commodityService.queryDetails(id);
			return comm;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 进入详情图片添加页面
	 */
	@RequestMapping("editcommodity_details")
	public String goeditcommodity_details(Integer id,Model model) {
		model.addAttribute("id",id);
		return "editcommodity_details";
	}
	
	/**
	 * 
	 * 异步添加详情图片
	 */
	@RequestMapping("addDetailsImg")
	@ResponseBody
	public String goaddDetailsImg(commodity_details comm_details) {
		try {
			commodityService.addDetailsImg(comm_details);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
	}
}
